package com.ssm.xml.interceptor;

//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @Date: Created in 9:40 2021/2/8
 */
@Configuration
//@EnableAutoConfiguration
public class CORSFilter {
    /**
     * 注册过滤器bean，设置跨域规则
     */
    @Bean
    public CorsFilter corsFilter() {
        //配置CORS相关规则
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许哪些域名可以跨域访问
        corsConfiguration.setAllowedOrigins(Arrays.asList("*"));
        //允许哪些提交方式可以访问
        corsConfiguration.setAllowedMethods(Arrays.asList("GET","PUT","DELETE","POST","OPTIONS"));
        //允许哪些请求头可以访问
        corsConfiguration.setAllowedHeaders(Arrays.asList("*"));
        //是否允许操作cookie
        corsConfiguration.setAllowCredentials(true);
        //设置预检请求有效期
        corsConfiguration.setMaxAge(1800L);
        //配置拦截请求路径
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }

}

